<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Passport extends Controller_Template{
	public $template = 'login';
	
	public function before()
	{
		parent::before();
		$this->load_sid();
	}
	public function action_login()
	{
		$username = '';
		$password = '';
		$rememberme = FALSE;
		$errinfo = '';
		$tips = '';
		
		if(Request::current()->method() == "POST")
		{
			$username = filter_input(INPUT_POST, "user_login");
			$password = filter_input(INPUT_POST, "user_pass");
		}
		
				
		if(!empty($remeberme)) $remeberme = TRUE;
		
		//检查用户名安全
		//security::sanitize_username($username);
		
		if(empty($errinfo))
		{
			if(empty($username))
			{
				$errinfo = '错误：用户名不合法！';
				$password = '';
			}
			else
			{//尝试登录
				
				$data['username'] = $username;
				$data['password'] = $password;
				//$data = json_encode($data, true);

				//request
				$uri = URL::edapi('login');
				$request = Request::factory($uri)->method('POST')->post($data);
				$request -> execute();
							
				if($request->response()->status()=='200')
				{
					$this->set_sid($request->response()->body('sid'));
					$this->set_user($username);
					Request::current() -> redirect('admin/adminframe');
					
				}
				else
				{
					$errinfo = '错误：用户名或密码不合法！';
					$password = '';
				}
			}
		}
		else
		{
		}
		
		//logout_done
		if($status = 'logout_done')
		{
			$tip = '提示：已经登出！';
			
		}
		
		//$view = View::factory('login');
		//$this->template->user_login = $username;
		//$this->template->user_pass = $password;
		//$this->template->checked = $rememberme;	
		//$this->template->render();
	}

	//logout
	public function action_logout()
	{
		$uri = url::edapi('logout');
		$sid = $this->get_sid();
		print_r($sid);exit;
		$request = Request::factory($uri)->method('GET')->post($sid);
		$request -> execute();
		Request::current() -> redirect('admin/login');
	}

} // End Controller_Passport
